<?php

class CursosController extends Site_Controller_Action
{
    public $limitRegistros = 4;

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/curso.js');
        
        $obj = new Ead_Model_DbTable_Catcurso();
        $this->view->filtro = $obj->fetchAll()->toArray();
        
        $where = 'C.CUR_STA = 1 and (C.CUR_VLD >= "'.date('Y-m-d').'" OR C.CUR_VLD = "0000-00-00") ';
        
        $obj = new Ead_Model_DbTable_Curso();
        $result = $obj->fetchAll($obj->select()->setIntegrityCheck(false)
                ->from(array('C'=>'CURSO'))->distinct()
                ->join(array('T'=>'CURSO_CAT'),'C.CUR_ID = T.CUR_ID')
                ->where($where)->limit($this->limitRegistros,0)
                ->group('C.CUR_ID')->order('CUR_ORD ASC'));
        //echo '<pre>',print_r($result);
        $this->view->result = $result; 
        
        $count = 0;
        $btn_mais = 1;
        foreach ($result as $res) $count += 1;
        if($count == 0){
            $btn_mais = 0;
        }else if($count < 4) {
            $btn_mais = 0;
        }
        
        $this->view->btn_mais = $btn_mais;
        
        $this->view->submenu_page = TRUE;
        
    }
    
    public function descricaoAction(){
        
        Zend_Loader::loadClass('tools','library');
        
        $id = $this->_getParam('id', 0);
        if ($id > 0) {
            $obj = new Ead_Model_DbTable_Curso();
            $row = $obj->fetchRow('CUR_ID = ' . $id);
            $this->view->row = $row;
            
            $obj = new Ead_Model_DbTable_Catcurso();
            $cat = $obj->fetchRow('CTC_ID = '.$row->CTC_ID);
            $this->view->categoria = $cat->CTC_NOM;
            $this->view->cat_css = $cat->CTC_CSS;
        }
    }
    public function comprarAction(){
        //$this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(); 
        
        $cur_id = $this->_getParam('id', 0);

        $sessao = new Zend_Session_Namespace('Carrinho');
        
        $arr = $sessao->carrinho; 
        
        if(count($arr) <= 1 && !is_array($arr)){
            $arr = array($cur_id);
        }
        
        if(array_search($cur_id, $arr) <= 0){
            array_push($arr, $cur_id);
        }
  
        $sessao->carrinho = $arr;
        
        $this->_helper->redirector('carrinho', 'pagamento', array());
            
             
    }
    public function filtroAction(){
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(); 
       
        //$where = 'CUR_STA = 1 and CUR_VLD >= "'.date('Y-m-d').'" ';
        $where = 'C.CUR_STA = 1 and (C.CUR_VLD >= "'.date('Y-m-d').'" OR C.CUR_VLD = "0000-00-00") ';
        $filtro = (int)$this->_getParam('filtro', 0);
        if($filtro > 0) $where .= ' and T.CTC_ID = '.$filtro;
        
        $obj = new Ead_Model_DbTable_Curso();
        //$result = $obj->fetchAll($obj->select()->where($where)->limit($this->limitRegistros,0));
        
        $result = $obj->fetchAll($obj->select()->setIntegrityCheck(false)
                ->from(array('C'=>'CURSO'))->distinct()
                ->join(array('T'=>'CURSO_CAT'),'C.CUR_ID = T.CUR_ID')
                ->where($where)->limit($this->limitRegistros,0)
                ->group('C.CUR_ID')->order('CUR_ORD ASC'));
        
        
        $count = 0;
        $btn_mais = 1;
        foreach ($result as $res) $count += 1;
        if($count == 0){
            $offset = 0;
            $btn_mais = 0;
        } else if($count < 4) {
            $btn_mais = 0;
        }
        
        $html =  $this->view->partial('cursos/list-cursos.phtml', array('urlbase' => $this->_urlbase,'result'=>$result));
        
        echo json_encode(array('ok' => true, 'html' => $html,'btn_mais'=>$btn_mais));
    }

    public function maismateriaisAction(){
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(); 
        
        $offset = (int)$this->_getParam('offset', 0) + $this->limitRegistros;
        
        $where = 'CUR_STA = 1 and (CUR_VLD >= "'.date('Y-m-d').'" OR CUR_VLD = "0000-00-00")';
        $filtro = (int)$this->_getParam('filtro', 0);
        if($filtro > 0) $where .= ' and CTC_ID = '.$filtro;
        
        $obj = new Ead_Model_DbTable_Curso();
        $result = $obj->fetchAll($obj->select()->where($where)->limit($this->limitRegistros,$offset));
        
        $count = 0;
        $btn_mais = 1;
        foreach ($result as $res) $count += 1;
        if($count == 0){
            $offset = $offset - $this->limitRegistros;
            $btn_mais = 0;
        }
        else if($count < 4) {
            $btn_mais = 0;
        }
        
        $html =  $this->view->partial('cursos/list-cursos.phtml', array('urlbase' => $this->_urlbase,'result'=>$result));
        
        echo json_encode(array('ok' => true, 'html' => $html,'offset'=>$offset,'btn_mais'=>$btn_mais));
        
    }
    
    
    
}

